package connection;

/**
 * Created by IntelliJ IDEA.
 * User: Qiang
 * Date: 27.11.2008
 * Time: 21:40:33
 *
 * This code ist in Allegro Example in Internet.
 */

import com.franz.ag.*;
import org.openrdf.model.Literal;
import org.openrdf.model.URI;


/**
 * This is a general utility class.
 * <p/>
 * Demonstrates some basics of using Cursors and extracting results.
 */
public class AGUtils {

    public static void showTriple(Triple tr) throws AllegroGraphException {
        System.out.println(tr.toString());
    }

    public static void showTriples(String msg, Cursor cc) throws AllegroGraphException {
        System.out.println(msg);
        showTriples(cc);
    }

    public static void showTriples(Cursor cc) throws AllegroGraphException {
        showTriples(cc, 100);
    }

    public static void showTriples(Cursor cc, int limit) throws AllegroGraphException {
        int i = 0;
        while (i < limit && cc.step()) {
            showTriple(cc.getTriple());
            i++;
        }
        if (cc.step()) {
            System.out.println("[Showing " + limit + " triples, there are more]");
        }
    }

    public static void showURI(URI r) {
        System.out.println(r.getURI() + ":");
        System.out.println("  Namespace: " + r.getNamespace());
        System.out.println("  LocalName: " + r.getLocalName());
    }

    public static void showLiteral(Literal lit) {
        System.out.println("Literal: " + lit.toString());
        System.out.println("  Label: " + lit.getLabel());
        System.out.println("  Datatype: " + lit.getDatatype());
        System.out.println("  Language: " + lit.getLanguage());
        // Note that only Literals added to the store have a UPI
        System.out.println("  UPI: " + ((ValueNode) lit).queryAGId());
    }

    public static void showEncodedLiteral(EncodedLiteral lit) {
        System.out.println("EncodedLiteral: " + lit.toString());
        System.out.println("  Label: " + lit.getLabel());
        System.out.println("  Datatype: " + lit.getDatatype());
        System.out.println("  Language: " + lit.getLanguage());
        // Note that only Literals added to the store have a UPI
        System.out.println("  UPI: " + ((ValueNode) lit).queryAGId());
    }

    public static void printObjectArray(String msg, Object[] objArr) {
        System.out.println(msg);
        if (objArr != null) {
            for (int i = 0; i < objArr.length; i++) {
                System.out.println("  " + i + ": " + objArr[i]);
            }
        }
    }

    public static void printUPIArray(String msg, AllegroGraph ts, UPI[] objArr) throws AllegroGraphException {
        System.out.println(msg);
        if (objArr != null) {
            for (int i = 0; i < objArr.length; i++) {
                String[] parts = ts.getParts(objArr[i]);
                System.out.println("  " + i + ": " + parts[1]);
            }
        }
    }

    public static String upiArrayToString(AllegroGraph ts, UPI[] objArr) throws AllegroGraphException {
        StringBuffer buf = new StringBuffer();
        if (objArr != null) {
            buf.append("{");
            for (int i = 0; i < objArr.length; i++) {
                String[] parts = ts.getParts(objArr[i]);
                buf.append(parts[1]).append(" ");
            }
            buf.append("}");
        }
        return buf.toString();
    }

    public static void printStringArray(String msg, String[] objArr) {
        System.out.println(msg);
        if (objArr != null) {
            for (int i = 0; i < objArr.length; i++) {
                System.out.println("  " + i + ": " + objArr[i]);
            }
        }
    }

    public static String printValueObject(ValueObject o) {
        String result;
        if (o == null) {
            result = "Null";
        } else if (o instanceof Literal) {
            Literal l = (Literal) o;
            result = l.getLabel();
        } else if (o instanceof BlankNode) {
            BlankNode b = (BlankNode) o;
            result = b.getID();
        } else if (o instanceof Node) {
            Node n = (Node) o;
            result = n.toString();
        } else {
            result = o.toString();
        }
        return result;
    }

    static void displayValues(ValueObject[][] v) {
        for (int i = 0; i < v.length; i++) {
            ValueObject[] objects = v[i];
            for (int j = 0; j < objects.length; j++) {
                System.out.println("[" + i + "][" + j + "]: " + objects[j]);
            }
        }
    }

    public static String elapsedTime(long start) {
        long total = System.currentTimeMillis() - start;
        long min = total / 60000;
        long msec = total % 60000;
        double sec = msec / 1000.0;
        String report;
        if (min > 0) {
            report = min + ":" + sec + " minutes:seconds";
        } else {
            report = sec + " seconds";
        }
        return report;
    }

}